from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import pandas as pd
import sqlite3
import csv


# 获取html
def get_html(types):
    driver = webdriver.Chrome()
    driver.get("https://data.eastmoney.com/hsgtcg/list.html")
    choice_type(types, driver)
    driver.implicitly_wait(10)
    return driver.page_source


# 类型选择
def choice_type(types, driver):
    global element
    if types == "北向":
        element = driver.find_element(By.XPATH, """//*[@id="mkt_type"]/li[1]""")
    if types == "沪股通":
        element = driver.find_element(By.XPATH, """//*[@id="mkt_type"]/li[2]""")
    if types == "深股通":
        element = driver.find_element(By.XPATH, """//*[@id="mkt_type"]/li[3]""")
    element.click()


# 获取元素标签列表
def parse(html):
    bs4 = BeautifulSoup(html, 'lxml')
    da = bs4.find(class_='dataview-body')
    da = da.find("tbody")
    data = da.find_all('tr')
    return data


# 爬取数据
def craw_data(data):
    result_data = []
    for i in data:
        datas_information = i.find_all('td')
        single_data = []

        # 代码
        ID = datas_information[1].find("a").text
        single_data.append(ID)

        # 名称
        name = datas_information[2].find("span").text
        single_data.append(name)

        # 今日收盘价
        today_close_price = datas_information[4].find("span").text
        single_data.append(today_close_price)

        # 今日涨跌幅
        today_rise_fall = datas_information[5].find("span").text
        single_data.append(today_rise_fall)

        # 今日持股数
        today_shareholding = datas_information[6].text
        single_data.append(today_shareholding)

        # 今日持股  市值
        today_market_value = datas_information[7].text
        single_data.append(today_market_value)

        # 今日持股  占流通股比
        today_outstanding_shares = datas_information[8].text
        single_data.append(today_outstanding_shares)

        # 今日持股  占总股本比
        today_total_equity = datas_information[9].text
        single_data.append(today_total_equity)

        # 今日增持估计  占总股本比
        today_increase_estimate = datas_information[10].text
        single_data.append(today_increase_estimate)

        # 今日增持估计  市值
        today_increase_estimate_market_value = datas_information[11].find('span').text
        single_data.append(today_increase_estimate_market_value)

        # 今日增持估计  市值增幅
        today_increase = datas_information[12].find('span').text
        single_data.append(today_increase)

        # 今日增持估计  占流通 股比
        today_increase_outstanding_shares = datas_information[13].find('span').text
        single_data.append(today_increase_outstanding_shares)

        # 今日增持估计  占总股本比
        today_increase_estimate_all = datas_information[14].find('span').text
        single_data.append(today_increase_estimate_all)

        # 今日增持估计  所属板块
        owning_plate = datas_information[15].find('a').text
        single_data.append(owning_plate)

        result_data.append(single_data)
    return result_data


# 存进csv文件
def save_data(result_data, types):
    title = ['代码', '名称', '今日收盘价', '今日涨跌幅',
             '今日持股_股数', '今日持股_市值', '今日持股_流通占比', '今日持股_占总股本比',
             '股数', '市值', '市值增幅', '占流通股比', '占总股本比', '板块']
    data = pd.DataFrame(result_data, columns=title)
    data.to_csv(types + "数据.csv")

# 存入数据
def create_insert_table():
    count = 0
    conn = sqlite3.connect('北向.db')
    c = conn.cursor()
    c.execute('''CREATE TABLE news
                 (title text, link text)''')
    with open('北向数据.csv', newline='', encoding='utf-8') as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            count += 1
            c.execute("INSERT INTO news VALUES (?, ?)", row)
    conn.commit()
    conn.close()
